package com.zwj.rms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.rms.bo.OperateBo;
import com.zwj.rms.dto.SysOperate;
import com.zwj.rms.entity.SysOperateEntity;
import com.zwj.rms.enums.IsDeletedEnum;
import com.zwj.rms.mapper.SysOperateMapper;
import com.zwj.rms.service.SysOperateService;
import com.zwj.rms.utils.DataCheckUtils;
import com.zwj.rms.utils.TreeUtils;
import com.zwj.rms.vo.Result;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 操作表 服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-09-15
 */
@Service
public class SysOperateServiceImpl extends ServiceImpl<SysOperateMapper, SysOperateEntity> implements SysOperateService {
    private final SysOperateMapper sysOperateMapper;

    public SysOperateServiceImpl(SysOperateMapper sysOperateMapper) {
        this.sysOperateMapper = sysOperateMapper;
    }
    @Override
    public Result selectOperate(OperateBo operateBo) {
        operateBo.setMenuIds("");
        operateBo.setMenuId("");
        //查询项目组评分 mybatis
        List<SysOperate> result = sysOperateMapper.selectOperate(operateBo);
        if (result.size() == 0) return Result.error().message("查询结果为空");
        if(result.size()==1){
            if(operateBo.getId()!=null && !operateBo.getId().equals("")){

                return Result.ok().data(result);
            }
            operateBo.setId(null);
            operateBo.setName(null);
            operateBo.setMenuIds(result.get(0).getMenuIds());
            operateBo.setMenuId(result.get(0).getId());
            result = sysOperateMapper.selectOperate(operateBo);
        }
        return Result.ok().data(TreeUtils.createOperateTree(result,"0"));
    }

    @Override
    public Result deleteOperate(OperateBo operateBo) {
        DataCheckUtils.isNotBlank(operateBo.getId(), "菜单id不能为空!");
        QueryWrapper<SysOperateEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(SysOperateEntity::getId, operateBo.getId()).eq(SysOperateEntity::getIsDeleted, IsDeletedEnum.NO.getKey());;
        SysOperateEntity operate = sysOperateMapper.selectOne(sectionQueryWrapper);
        //查询结果校验
        if (operate == null) {
            return Result.error().message("菜单不存在！");
        }
        operateBo.setMenuId(operate.getId().toString());
        //查询项目组评分 mybatis
        int row = sysOperateMapper.deleteOperate(operateBo);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("删除菜单失败");
        }
        return Result.ok();
    }
}
